<template>
  <div><h1>商户审核</h1></div>
  <div>
    <el-form :inline="true" :model="searForm" class="demo-form-inline">
      <el-form-item label="关键字">
        <el-input
          v-model="searForm.keyWord"
          placeholder="请输入关键字"
          clearable
        />
      </el-form-item>

      <el-form-item label="性别">
        <el-select
          v-model="searForm.typeId"
          style="width: 80px"
          placeholder="请选择性别"
          clearable
        >
          <el-option label="男" :value="0" />
          <el-option label="女" :value="1" />
        </el-select>
      </el-form-item>

      <el-form-item label="开始时间">
        <el-date-picker
          v-model="searForm.beginTime"
          type="datetime"
          placeholder="请选择开始时间"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>

      <el-form-item label="结束时间">
        <el-date-picker
          v-model="searForm.endTime"
          type="datetime"
          placeholder="请选择结束时间"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onQuery">查询</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column prop="userId" label="商户ID" />
      <el-table-column prop="username" label="商户账号" />
      <el-table-column prop="mobile" label="手机号" />
      <el-table-column prop="checkStatus" label="审核状态">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.checkStatus == 0"
            >待审核</el-tag
          >
          <el-tag type="success" v-if="scope.row.checkStatus == 1"
            >审核通过</el-tag
          >
          <el-tag type="danger" v-if="scope.row.checkStatus == 2"
            >审核驳回</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="realName" label="真实姓名" />
      <el-table-column prop="sex" label="性别">
        <template #default="scope">
          <el-tag type="primary" v-if="scope.row.sex == 0">男</el-tag>
          <el-tag type="primary" v-if="scope.row.sex == 1">女</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="注册时间" />
      <el-table-column prop="checkReason" label="审核原因" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" type="primary" @click="onCheck(scope.row)">
            审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-pagination
      v-model:current-page="searForm.pageNum"
      v-model:page-size="searForm.pageSize"
      :page-sizes="[2, 4, 6, 8]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <!-- 弹窗 ---------------------------------------------------------->
  <div>
    <el-dialog v-model="dialogFormVisible" title="商户审核" width="500">
      <el-form :model="form">
        <el-form-item label="登录账号" :label-width="formLabelWidth">
          <el-input v-model="form.username" disabled autocomplete="off" />
        </el-form-item>

        <el-form-item label="真实姓名" :label-width="formLabelWidth">
          <el-input v-model="form.realName" disabled autocomplete="off" />
        </el-form-item>

        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="form.mobile" disabled autocomplete="off" />
        </el-form-item>

        <el-form-item label="性别" :label-width="formLabelWidth">
          <el-select
            v-model="form.sex"
            placeholder="请选择性别"
            style="width: 150px"
            clearable
            disabled
          >
            <el-option label="男" :value="0" />
            <el-option label="女" :value="1" />
          </el-select>
        </el-form-item>

        <el-form-item label="审核" :label-width="formLabelWidth">
          <el-switch
            v-model="form.checkStatus"
            active-text="通过"
            inactive-text="驳回"
            :active-value="1"
            :inactive-value="2"
          />
        </el-form-item>

        <el-form-item label="审核原因" :label-width="formLabelWidth">
          <el-input
            v-model="form.checkReason"
            type="textarea"
            autocomplete="off"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">放弃</el-button>
          <el-button type="primary" @click="onsubmitCheckUser">
            提交
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { getUserlist, checkUser } from "../api/api";
import { ElMessage } from "element-plus";
import { load } from "@amap/amap-jsapi-loader";

const tableData = ref([]);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const multipleSelection = ref<any[]>([]);
const handleSelectionChange = (val: any[]) => {
  multipleSelection.value = val;
  console.log(multipleSelection.value);
};

const form = ref({
  avatar: "",
  checkReason: "",
  checkStatus: null,
  createTime: "",
  email: "",
  mobile: "",
  password: "",
  realName: "",
  sex: null,
  token: "",
  userId: 0,
  username: "",
  typeId: null,
});

const searForm = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: null,
  typeId: null,
  beginTime: "",
  endTime: "",
});
const total = ref(0);
const loadList = () => {
  getUserlist(searForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searForm.value.pageSize = val;
  loadList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searForm.value.pageNum = val;
  loadList();
};

const onQuery = () => {
  console.log("submit!");
  loadList();
};

const onCheck = (row: any) => {
  console.log(row);
  dialogFormVisible.value = true;
  Object.assign(form.value, row);
};

/**
 * 审核
 */
const onsubmitCheckUser = () => {
  if (form.value.checkStatus == 2) {
    if (form.value.checkReason == null || form.value.checkReason == "") {
      ElMessage.error("驳回原因不能为空");
      return;
    }
  }
  checkUser(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("审核通过");
      dialogFormVisible.value = false;
      loadList();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};

onMounted(() => {
  loadList();
});
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
